/**
 * 
 */
package kit4j.ui.cs.swing.dnd;

import java.awt.Component;
import java.awt.Container;
import java.awt.datatransfer.DataFlavor;

import javax.swing.DropMode;
import javax.swing.TransferHandler;

/**
 * 此类用于提供处理Component对象在Container对象之间的移动操作的默认处理。该TransferHandler
 * 
 * @author MiniKnife
 * 
 */
public class ComponentMoveTransferHandler extends TransferHandler {

	private static final long serialVersionUID = -7499255953104510414L;

	private DataFlavor supportDataFlavor = null;

	private Container container = null;

	private AbstractComponentDropper dropper = null;

	private DropMode dropMode = null;

	public ComponentMoveTransferHandler(Container container) {
		this(container, DropMode.ON);
	}

	public ComponentMoveTransferHandler(Container container, DropMode mode) {
		super();

		this.container = container;
		setDropMode(mode);

		try {
			supportDataFlavor = new DataFlavor(
					DataFlavor.javaJVMLocalObjectMimeType + ";class="
							+ Component.class.getCanonicalName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean canImport(TransferSupport support) {
		return support.isDrop() && MOVE == support.getDropAction()
				&& support.isDataFlavorSupported(supportDataFlavor);
	}

	@Override
	public boolean importData(TransferSupport support) {
		return dropper.dropComponent(support);
	}

	/**
	 * 设置放置模式。该属性设置将决定组件拖放到容器上时事件的响应效果。
	 * 
	 * @param mode
	 *            要设置的放置模式
	 */
	public void setDropMode(DropMode mode) {
		if (this.dropMode != null && this.dropMode.equals(mode)) {
			return;
		}

		this.dropMode = mode;

		switch (mode) {
		case ON:
			dropper = new OnComponentDropper(container);
			break;
		case INSERT:
			dropper = new InsertComponentDropper(container);
			break;
		default:
			dropper = new OnComponentDropper(container);
			break;
		}
	}

}